<?php
/**
 * This file is part of won-uploader.
 *
 * Author: Daniel K.
 *
 * won-uploader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * won-uploader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with won-uploader. If not, see <http://www.gnu.org/licenses/>.
 */
   
   if( !defined("won-uploader")) die();    // Silence is golden
   
   class Paths
   {
       private /* string */     $appDir;
       private /* string */     $httpAppDir;
       
       /**
        * Creates a new instance of <code>Paths</code>
        */
       public function __construct()
       {
           $this->appDir = dirname( dirname( __FILE__ ) );
           $this->httpAppDir = dirname( $_SERVER['PHP_SELF'] );
       }
       
       /**
        * Returns the absolute path of the application
        * @return   string  Path 
        */
       public function getAppDirectory()
       {
           return $this->appDir;       
       }
       
       /**
        * Returns the absolute HTTP path of the application
        * @return   string  HTTP path
        */
       public function getHTTPAppDirectory()
       {
           return $this->httpAppDir."/";
       }
       
       /**
        * Returns the URL of the app including protocol (http/https) and
        * full path
        * @return   String  URL
        */
       public function getHTTPURL()
       {
           return $this->getProtocol()."://".$_SERVER['SERVER_NAME'].$this->getHTTPAppDirectory();
       }
       
       /**
        * Returns the absolute path to a class file
        * @param  string $classname Valid class name
        * @return string			Absolute path
        */
       public function getIncludePath($classname)
       {
           $this->validate($classname);
           return $this->appDir."/lib/$classname.lib.php";
       }
       
       /**
        * Returns the path to the config file
        * @return string	Absolute path
        */
       public function getConfigFile()
       {
           return $this->appDir."/config.php";
       }
       
       public function getLanguageFile($language)
       {
       	   return $this->appDir."/lib/languages/$language.lang.php"; 
       }
       
       /**
        * Returns the path to the login file
        * @return string	Absolute path
        */
       public function getLoginPath()
       {
           return $this->appDir."/login.php";
       }
       
       /**
        * Returns the path to the storage configuration file containig
        * the path of the stored files
        * @return string	Absolute path
        */
       public function getStorageConfigFile()
       {
           return $this->appDir."/storage.php";
       }
       
       /**
        * Returns the path to the template
        * @param string $type	Template name, e.g. 'main'
        */
       public function getTemplate($type)
       {
       	if( $type == "" )	return "";
       	 
       	return $this->appDir."/templates/$type.tpl.php";
       }
       
       // Validate class name
       private function validate($classname)
       {
           if( preg_match("/[^a-zA-Z0-9_]/", $classname) )
            throw new Exception("Invalid class name");
       }
       
       // 'http' or 'https'?
       private function getProtocol()
       {
           return ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == "on" ) ? "https" : "http";
       }
   }
?>